﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class DALBatteryCenter : IDALBatteryCenter, IDisposable
    {
        private CarBatteryEntities context;
        private bool disposed = false;

        public DALBatteryCenter(CarBatteryEntities context)
        {
            this.context = context;
        }

        public BatteryStation FindBatteryStation(string name)
        {
            return context.BatteryStation.FirstOrDefault(x => x.name.Equals(name));
        }

        public IEnumerable<BatteryStation> GetAllStations()
        {
            return context.BatteryStation.AsEnumerable();
        }

        public BatteryStation GetBatteryStationByID(int batStatID)
        {
            return context.BatteryStation.Find(batStatID);
        }

        public void InsertBatteryStation(BatteryStation batStat)
        {
            context.BatteryStation.Add(batStat);
        }

        public void DeleteBatteryStation(int batStatID)
        {
            context.BatteryStation.Remove(GetBatteryStationByID(batStatID));
        }

        public void UpdateBatteryStation(BatteryStation batStat)
        {
            context.Entry(batStat).State = EntityState.Modified;
        }

        public int Save()
        {
            return context.SaveChanges();
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                    context.Dispose();
            }

            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}